import { ref } from 'vue'
import type { Ref } from 'vue'
import axios, { AxiosResponse } from 'axios'

export function useRequest<T = any>() {
  const loading = ref(false)
  const data = ref<T>()
  const error = ref<Error>()
  
  const controller = new AbortController()
  
  const execute = async (promise: Promise<AxiosResponse<T>>) => {
    loading.value = true
    error.value = undefined
    
    try {
      const response = await promise
      data.value = response.data
      return response
    } catch (err: any) {
      error.value = err
      throw err
    } finally {
      loading.value = false
    }
  }
  
  const cancel = () => {
    controller.abort()
  }
  
  return {
    loading,
    data,
    error,
    execute,
    cancel,
    signal: controller.signal
  }
} 